import 'package:flutter_admin/api/odoo/dynamic_data_api.dart';
import 'package:flutter_admin/models/odoo/dms_userqualifications.dart';
import 'package:flutter_admin/models/user_info.dart';
import 'package:flutter_admin/pages/common/cry/cry_all.dart';
import 'package:flutter_admin/pages/common/cry/vo/select_option_vo.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_admin/pages/common/cry/cry_button.dart';
import 'package:flutter_admin/utils/store_util.dart';
import '../../../generated/l10n.dart';

class BaseDictionarydataDialog extends StatefulWidget {
  BaseDictionarydataDialog();

  @override
  _BaseDictionarydataDialogState createState() =>
      _BaseDictionarydataDialogState();
}

class _BaseDictionarydataDialogState extends State<BaseDictionarydataDialog> {
  // late List<Widget> children;
  final GlobalKey<FormState> formKey = GlobalKey<FormState>();

  DmsUserqualifications dmsUserqualifications = DmsUserqualifications();

  // 部门
  late List<SelectOptionVO> locationList = [];
  // 证书类别
  late List<SelectOptionVO> certificatetypeList = [];
  late List<SelectOptionVO> stateList = [];

  @override
  void initState() {
    super.initState();
    _loadData();
  }

  _loadData() async {
    stateList = [
      SelectOptionVO(value: '0', label: "未过期"),
      SelectOptionVO(value: '1', label: "已过期"),
    ];
    UserInfo userInfo = StoreUtil.getCurrentUserInfo();
    if (userInfo.major != null) {
      locationList = await DynamicDataApi.getModelSelect(
          active: false,
          model: "stock.location",
          name: "complete_name",
          key: 'workshop_id',
          val: null,
          key2: 'comment',
          val2: "CPK");
    } else {
      locationList = await DynamicDataApi.getModelSelect(
          active: false,
          model: "stock.location",
          name: "complete_name",
          key: 'workshop_id',
          val: int.parse(userInfo.deptId!),
          key2: 'comment',
          val2: "CPK");
    }
    certificatetypeList = await DynamicDataApi.getDictList("certificatetype");
    setState(() {});
  }

  @override
  Widget build(BuildContext context) {
    var form = Form(
      key: formKey,
      child: Wrap(
        children: <Widget>[
          CrySelect(
            width: 280,
            label: '部门',
            dataList: locationList,
            value: dmsUserqualifications.departmentId,
            onSaved: (v) => {dmsUserqualifications.departmentId = v},
          ),
          CrySelect(
            width: 280,
            label: '证书类别',
            dataList: certificatetypeList,
            value: dmsUserqualifications.certificatetypeId,
            onSaved: (v) => {dmsUserqualifications.certificatetypeId = v},
          ),
          CryInput(
            width: 280,
            label: '证书编号',
            value: dmsUserqualifications.certificateno,
            onSaved: (v) {
              dmsUserqualifications.certificateno = v;
            },
          ),
          CryInput(
            width: 280,
            label: '证书名称',
            value: dmsUserqualifications.certificatename,
            onSaved: (v) {
              dmsUserqualifications.certificatename = v;
            },
          ),
          CryInput(
            width: 280,
            label: '证书持有人',
            value: dmsUserqualifications.holderName,
            onSaved: (v) {
              dmsUserqualifications.holderName = v;
            },
          ),
          CrySelect(
            width: 280,
            label: '状态',
            dataList: stateList,
            value: dmsUserqualifications.state,
            onSaved: (v) => {dmsUserqualifications.state = v},
          ),
        ],
      ),
    );
    var buttonBar = ButtonBar(
      alignment: MainAxisAlignment.center,
      children: <Widget>[
        CryButton(
          label: "确定",
          onPressed: () {
            FormState form = formKey.currentState!;
            form.save();
            Navigator.pop(context, dmsUserqualifications);
          },
          iconData: Icons.save,
        ),
        CryButton(
          label: S.of(context).cancel,
          onPressed: () {
            Navigator.pop(context);
          },
          iconData: Icons.cancel,
        )
      ],
    );
    var result = Scaffold(
      appBar: AppBar(
        title: Text("查询"),
      ),
      body: SingleChildScrollView(
        child: Column(
          children: [form],
        ),
      ),
      bottomNavigationBar: buttonBar,
    );
    return SizedBox(
      width: 650,
      height: 650,
      child: result,
    );
    // return result;
  }
}
